package org.szgb.console.ht.electricity.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class HtElectricityUsage extends BeanEntity<HtElectricityUsage> {
    @Serial
    private static final long serialVersionUID = 1L;
    private HtElectricityAmmeter ammeter;
    @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM")
    private LocalDate month;
    private Integer electricity;

    public HtElectricityUsage() {
    }

    public HtElectricityUsage(String id) {
        this.setId(id);
    }

    public HtElectricityUsage(HtElectricityUsage.HtElectricityUsageFindDto htElectricityUsageFindDto) {
        this.setId(htElectricityUsageFindDto.getId());
        this.setAmmeter(new HtElectricityAmmeter(htElectricityUsageFindDto.getAmmeterId()));
        if (htElectricityUsageFindDto.getMonth() != null) {
            this.setMonth(htElectricityUsageFindDto.getMonth().atDay(1));
        }
        this.setPageDto(htElectricityUsageFindDto.getPageDto());
    }

    public HtElectricityUsage(HtElectricityUsage.HtElectricityUsageSaveDto htElectricityUsageSaveDto) {
        this.setId(htElectricityUsageSaveDto.id);
        this.setAmmeter(new HtElectricityAmmeter(htElectricityUsageSaveDto.ammeterId));
        if (htElectricityUsageSaveDto.month != null) {
            this.setMonth(htElectricityUsageSaveDto.month.atDay(1));
        }
        this.setElectricity(htElectricityUsageSaveDto.electricity);
    }

    public HtElectricityUsage.HtElectricityUsageView toHtElectricityUsageView() {
        return new HtElectricityUsage.HtElectricityUsageView(this);
    }

    @Data
    @Schema(title = "结算电量信息", description = "结算电量信息")
    public static class HtElectricityUsageView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "电表id")
        private String ammeterId;
        @Schema(description = "电表名称")
        private String ammeterName;
        @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM")
        @Schema(description = "月份(格式yyyy-MM)")
        private YearMonth month;
        @Schema(description = "结算电量")
        private Integer electricity;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public HtElectricityUsageView(HtElectricityUsage htElectricityUsage) {
            this.id = htElectricityUsage.getId();
            this.ammeterId = htElectricityUsage.getAmmeter().getId();
            this.ammeterName = htElectricityUsage.getAmmeter().getName();
            if (htElectricityUsage.getMonth() != null) {
                this.month = YearMonth.from(htElectricityUsage.getMonth());
            }
            this.electricity = htElectricityUsage.getElectricity();
            this.createdAt = htElectricityUsage.getCreatedAt();
            this.updatedAt = htElectricityUsage.getUpdatedAt();
            if (htElectricityUsage.getCreatedBy() != null) {
                this.createdById = htElectricityUsage.getCreatedBy().getId();
                this.createdByName = htElectricityUsage.getCreatedBy().getName();
            }
            if (htElectricityUsage.getUpdatedBy() != null) {
                this.updatedById = htElectricityUsage.getUpdatedBy().getId();
                this.updatedByName = htElectricityUsage.getUpdatedBy().getName();
            }
        }

        public static List<HtElectricityUsage.HtElectricityUsageView> getViewListFromList(List<HtElectricityUsage> htElectricityUsageList) {
            return htElectricityUsageList.stream().map(HtElectricityUsage.HtElectricityUsageView::new).toList();
        }
    }

    @Data
    @Schema(title = "结算电量查询表单", description = "结算电量查询参数")
    public static class HtElectricityUsageFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "电表id")
        private String ammeterId;
        @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM")
        @Schema(description = "月份(格式yyyy-MM)")
        private YearMonth month;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public HtElectricityUsage toHtElectricityUsage() {
            return new HtElectricityUsage(this);
        }
    }

    @Data
    @Schema(title = "结算电量保存表单", description = "结算电量保存参数")
    public static class HtElectricityUsageSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "电表id")
        private String ammeterId;
        @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM")
        @Schema(description = "月份(格式yyyy-MM)")
        private YearMonth month;
        @Schema(description = "结算电量")
        private Integer electricity;

        public HtElectricityUsage toHtElectricityUsage() {
            return new HtElectricityUsage(this);
        }
    }
}
